cc.Class({
    extends: cc.Component,

    properties: {
        gotoLabel: {
            default: null,
            type: cc.Node,
        },
        getLabel: {
            default: null,
            type: cc.Node,
        },
        hasGetLabel: {
            default: null,
            type: cc.Node,
        },
        enterGiftLayer: {
            default: null,
            type: cc.Node,
        }
    },



    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        this.hasGetGift = false
        this.canGetGiftTime = false

        if (cc.sys.platform === cc.sys.Platform.BYTEDANCE_MINI_GAME) {
            this.hasGetGift = getDay() === wx.getStorageSync('has_get_today_gift');
            if (!tt.navigateToScene) {
                this.enterGiftButton.active = false
            }
        }
        this.onShowCallback();
    },
    onEnable() {
        if (this.hasGetGift) {
            this.getLabel.active = false
            this.gotoLabel.active = false
            this.hasGetLabel.active = true
        } else {
            this.getLabel.active = false
            this.gotoLabel.active = true
            this.hasGetLabel.active = false
        }
    },
    onShowCallback() {
        let self = this
        if (cc.sys.platform === cc.sys.Platform.BYTEDANCE_MINI_GAME) {
            wx.onShow(function(t) {
                if (t.launch_from === 'homepage') {
                    console.log('奖励')
                    var date = getDay()
                    var last = wx.getStorageSync('entry_gift_time');
                    if (last !== date) {
                        self.canGetGiftTime = true;
                        wx.setStorageSync('entry_gift_time', date);
                        self.getLabel.active = true
                        self.gotoLabel.active = false
                        self.hasGetLabel.active = false
                        self.enterGiftNode.active = true
                    }
                }
            })
        }
    },
    gotoScene() {
        if (this.hasGetGift) {
            wx.showToast({
                title: '今天已经领取，明天再来吧',
                icon: 'none'
            });
            this.hideEnterGift()
            return;
        }
        if (this.canGetGiftTime) {
            // cc.app.system.gameSystem.diamond += 1;
            // window.powerEvent.emit('power-change');
            wx.showToast({
                title: '恭喜获得1个道具',
                icon: 'none'
            });
            wx.setStorageSync('has_get_today_gift', getDay());
            this.hasGetGift = true;
            this.hideEnterGift()
            return;
        }
        if (tt.navigateToScene) {
            tt.navigateToScene({
                scene: 'sidebar',
                fail: console.log,
                success: console.log
            })
        }
        this.hideEnterGift()
    },
    showEnterGift(){
        this.enterGiftLayer.active = true
    },
    hideEnterGift(){
        this.enterGiftLayer.active = false
    }

    // update (dt) {},
});

function getDay() {
    // 获取当前日期
    var today = new Date();

    // 获取年、月、日
    var year = today.getFullYear();
    var month = today.getMonth() + 1; // 月份从 0 开始，所以需要加 1
    var day = today.getDate();

    // 将月份和日期补零
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }

    // 构建 YYYY-MM-DD 格式的日期字符串
    var formattedDate = year + '-' + month + '-' + day;

    return formattedDate

}